// $ANTLR 2.7.7 (2006-11-01): "grammar/twelf.g" -> "TwelfParser.java"$

/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.twelfparser;

import java.util.List;
import java.util.LinkedList;

import com.googlecode.twelfdevtools.twelfast.*;

import antlr.TokenBuffer;
import antlr.TokenStreamException;
import antlr.TokenStreamIOException;
import antlr.ANTLRException;
import antlr.LLkParser;
import antlr.Token;
import antlr.TokenStream;
import antlr.RecognitionException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.ParserSharedInputState;
import antlr.collections.impl.BitSet;

public class TwelfParser extends antlr.LLkParser       implements TwelfParserTokenTypes
 {

protected TwelfParser(TokenBuffer tokenBuf, int k) {
  super(tokenBuf,k);
  tokenNames = _tokenNames;
}

public TwelfParser(TokenBuffer tokenBuf) {
  this(tokenBuf,3);
}

protected TwelfParser(TokenStream lexer, int k) {
  super(lexer,k);
  tokenNames = _tokenNames;
}

public TwelfParser(TokenStream lexer) {
  this(lexer,3);
}

public TwelfParser(ParserSharedInputState state) {
  super(state,3);
  tokenNames = _tokenNames;
}

	public final List<ITwelfDeclaration>  startRule() throws RecognitionException, TokenStreamException {
		List<ITwelfDeclaration> decls;
		
		decls = new LinkedList<ITwelfDeclaration>(); ITwelfDeclaration d;
		
		try {      // for error handling
			{
			_loop3:
			do {
				if ((LA(1)==ID||LA(1)==LITERAL__)) {
					d=decl();
					decls.add(d);
				}
				else {
					break _loop3;
				}
				
			} while (true);
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_0);
		}
		return decls;
	}
	
	public final ITwelfDeclaration  decl() throws RecognitionException, TokenStreamException {
		ITwelfDeclaration decl;
		
		decl = null;
		
		try {      // for error handling
			decl=constant();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_1);
		}
		return decl;
	}
	
	public final ConstantDeclaration  constant() throws RecognitionException, TokenStreamException {
		ConstantDeclaration decl;
		
		Token  c = null;
		Token  e1 = null;
		Token  b = null;
		Token  e2 = null;
		ITwelfTerm t = null, v = null; decl = null;
		
		try {      // for error handling
			switch ( LA(1)) {
			case ID:
			{
				c = LT(1);
				match(ID);
				{
				switch ( LA(1)) {
				case 5:
				{
					match(5);
					t=term();
					{
					switch ( LA(1)) {
					case 6:
					{
						match(6);
						v=term();
						break;
					}
					case 7:
					{
						break;
					}
					default:
					{
						throw new NoViableAltException(LT(1), getFilename());
					}
					}
					}
					break;
				}
				case 6:
				{
					match(6);
					v=term();
					break;
				}
				default:
				{
					throw new NoViableAltException(LT(1), getFilename());
				}
				}
				}
				e1 = LT(1);
				match(7);
				Identifier w = new Identifier(c.getText(), c.getLine(), c.getColumn()); decl = ConstantDeclaration.namedConst(w, t, v, c.getLine(), c.getColumn(), e1.getLine(), e1.getColumn() + 1);
				break;
			}
			case LITERAL__:
			{
				b = LT(1);
				match(LITERAL__);
				{
				switch ( LA(1)) {
				case 5:
				{
					match(5);
					t=term();
					break;
				}
				case 6:
				{
					break;
				}
				default:
				{
					throw new NoViableAltException(LT(1), getFilename());
				}
				}
				}
				match(6);
				v=term();
				e2 = LT(1);
				match(7);
				decl = ConstantDeclaration.anonConst(t, v, b.getLine(), b.getColumn(), e2.getLine(), e2.getColumn() + 1);
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_1);
		}
		return decl;
	}
	
	public final ITwelfTerm  term() throws RecognitionException, TokenStreamException {
		ITwelfTerm t;
		
		t = null;
		
		try {      // for error handling
			t=pi_term();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_2);
		}
		return t;
	}
	
	public final ITwelfTerm  pi_term() throws RecognitionException, TokenStreamException {
		ITwelfTerm result;
		
		Token  x = null;
		Token  c = null;
		ITwelfTerm vt = null, t; result = null;
		
		try {      // for error handling
			switch ( LA(1)) {
			case 9:
			{
				match(9);
				x = LT(1);
				match(ID);
				{
				switch ( LA(1)) {
				case 5:
				{
					match(5);
					vt=term();
					break;
				}
				case 10:
				{
					break;
				}
				default:
				{
					throw new NoViableAltException(LT(1), getFilename());
				}
				}
				}
				match(10);
				t=pi_term();
				Identifier v = new Identifier(x.getText(), x.getLine(), x.getColumn()); result = Pi.pi(v, vt, t);
				break;
			}
			case 11:
			{
				match(11);
				c = LT(1);
				match(ID);
				{
				switch ( LA(1)) {
				case 5:
				{
					match(5);
					vt=term();
					break;
				}
				case 12:
				{
					break;
				}
				default:
				{
					throw new NoViableAltException(LT(1), getFilename());
				}
				}
				}
				match(12);
				t=pi_term();
				Identifier w = new Identifier(c.getText(), c.getLine(), c.getColumn()); result = Lambda.lambda(w, vt, t);
				break;
			}
			case ID:
			case LITERAL__:
			case LITERAL_type:
			case 16:
			{
				result=ascr_term();
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_2);
		}
		return result;
	}
	
	public final ITwelfTerm  ascr_term() throws RecognitionException, TokenStreamException {
		ITwelfTerm result;
		
		ITwelfTerm t, a; result = null;
		
		try {      // for error handling
			t=arr_term();
			{
			switch ( LA(1)) {
			case 5:
			{
				match(5);
				a=term();
				result = Ascription.ascr(t, a);
				break;
			}
			case 6:
			case 7:
			case 10:
			case 12:
			case 17:
			{
				result = t;
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_2);
		}
		return result;
	}
	
	public final ITwelfTerm  arr_term() throws RecognitionException, TokenStreamException {
		ITwelfTerm result;
		
		ITwelfTerm t1, t2; result = null; LinkedList<ITwelfTerm> stack = new LinkedList<ITwelfTerm>();
		
		try {      // for error handling
			t1=app_term();
			{
			_loop17:
			do {
				switch ( LA(1)) {
				case 13:
				{
					match(13);
					t2=app_term();
					stack.add(t1); t1 = t2;
					break;
				}
				case 14:
				{
					match(14);
					t2=app_term();
					t1 = Arrow.backArr(t1, t2);
					break;
				}
				default:
				{
					break _loop17;
				}
				}
			} while (true);
			}
			stack.add(t1); result = Arrow.forwArr(stack);
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_3);
		}
		return result;
	}
	
	public final ITwelfTerm  app_term() throws RecognitionException, TokenStreamException {
		ITwelfTerm result;
		
		ITwelfTerm t2; result = null;
		
		try {      // for error handling
			result=atom_term();
			{
			_loop20:
			do {
				if ((_tokenSet_4.member(LA(1)))) {
					t2=atom_term();
					result = Application.app(result, t2);
				}
				else {
					break _loop20;
				}
				
			} while (true);
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_5);
		}
		return result;
	}
	
	public final ITwelfTerm  atom_term() throws RecognitionException, TokenStreamException {
		ITwelfTerm result;
		
		Token  w = null;
		Token  x = null;
		result = null;
		
		try {      // for error handling
			switch ( LA(1)) {
			case LITERAL_type:
			{
				match(LITERAL_type);
				result = Type.type();
				break;
			}
			case LITERAL__:
			{
				w = LT(1);
				match(LITERAL__);
				result = new Wildcard(w.getLine(), w.getColumn());
				break;
			}
			case ID:
			{
				x = LT(1);
				match(ID);
				result = new Identifier(x.getText(), x.getLine(), x.getColumn());
				break;
			}
			case 16:
			{
				match(16);
				result=term();
				match(17);
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_6);
		}
		return result;
	}
	
	
	public static final String[] _tokenNames = {
		"<0>",
		"EOF",
		"<2>",
		"NULL_TREE_LOOKAHEAD",
		"ID",
		"\":\"",
		"\"=\"",
		"\".\"",
		"\"_\"",
		"\"{\"",
		"\"}\"",
		"\"[\"",
		"\"]\"",
		"\"->\"",
		"\"<-\"",
		"\"type\"",
		"\"(\"",
		"\")\"",
		"CONTROL",
		"COMMENT",
		"PRAGMA",
		"WS"
	};
	
	private static final long[] mk_tokenSet_0() {
		long[] data = { 2L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
	private static final long[] mk_tokenSet_1() {
		long[] data = { 274L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
	private static final long[] mk_tokenSet_2() {
		long[] data = { 136384L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_2 = new BitSet(mk_tokenSet_2());
	private static final long[] mk_tokenSet_3() {
		long[] data = { 136416L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_3 = new BitSet(mk_tokenSet_3());
	private static final long[] mk_tokenSet_4() {
		long[] data = { 98576L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_4 = new BitSet(mk_tokenSet_4());
	private static final long[] mk_tokenSet_5() {
		long[] data = { 160992L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_5 = new BitSet(mk_tokenSet_5());
	private static final long[] mk_tokenSet_6() {
		long[] data = { 259568L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_6 = new BitSet(mk_tokenSet_6());
	
	}
